<?php

/* @var $this yii\web\View */
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\ChargeCashDeal;
use yii\data\ActiveDataProvider;
use yii\helpers\Url;

if($mode=='ok'){
  $title = '会员充值成功记录';
  $status = ChargeCashDeal::STATUS_PROVED;
}else if($mode=='fail'){
  $title = '会员充值失败记录';
  $status[] = ChargeCashDeal::STATUS_UNPROVED;
  $status[] = ChargeCashDeal::STATUS_DISCARD;
}else{
  echo "非法请求";
  return;
}
$dataProvider = new ActiveDataProvider([
    'query' => ChargeCashDeal::find()->select(['id','money','status','member_id','approve_dt','reg_dt','channel'])->where(
      [
        'type' => ChargeCashDeal::TYPE_CHARGE,
        'status'=>$status
      ])->with('member'),
    'pagination' => [
        'pageSize' => 20,
    ],
]);

$this->title = $title;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="page-title">
  <div>
    <h1><i class="fa fa-money"></i> <?=$title?></h1>
    <ul class="breadcrumb side">
      <li><i class="fa fa-home fa-lg"></i></li>
      <li>会员充值和提现管理</li>
      <li class="active"><a href="#"><?=$title?></a></li>
    </ul>
  </div>
</div>

    <div class="card">
      <?php
      //如何提前加载资源
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    // 数据提供者中所含数据所定义的简单的列
    // 使用的是模型的列的数据
    'member.realname',
    [
      'header'=>'申请日期',
      'content'=>function($model,$key,$index,$column){
        $output=Yii::$app->formatter->asDate($model->reg_dt,'yyyy-MM-dd');
        return $output;
      }
    ],
    [
      'header'=>'充值通道',
      'content'=>function ($model, $key, $index, $column) {
          switch($model->channel){
            case '1':
            return '网银';
            case '2':
            return '支付宝';
            case '3':
            return '微信';
            default:
            return '未知';
          }
      },
    ],
    [
      'header'=>'充值金额',
      'content'=>function ($model, $key, $index, $column) {
          return number_format($model->money,2);
      },
    ],
    [
        'header'=>'充值状态',
        'content'=>function($model, $key, $index, $column)
        {
          switch($model->status){
            case ChargeCashDeal::STATUS_REGISTER:
            return '<i class="fa fa-lg fa-gavel"></i>';
            case ChargeCashDeal::STATUS_PROVED:
            return '成功 <i class="fa fa-lg fa-check-circle alert-info"></i>';
            case ChargeCashDeal::STATUS_UNPROVED:
            return '失败 <i class="fa fa-lg fa-times-circle alert-danger"></i>';
            case ChargeCashDeal::STATUS_DISCARD:
            return '作废 <i class="fa fa-lg fa-trash alert-default"></i> <a class="prove_lost_congzhi" style="cursor:pointer;"><i class="fa fa-lg fa-edit alert-info"></i>补审</a>';
            default:
            return '作废 <i class="fa fa-lg fa-trash alert-default"></i>';
          }
        }
    ],
],

]);
?>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header btn-warning">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><i class="fa fa-legal"></i> 充值审核</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-md-12">
            <!--div class="card"-->
              <section class="invoice">
                <div class="row">
                  <div class="col-xs-12">
                    <h3 class="page-header"><i class="fa fa-user"></i> <span id='reg_realname'>老虎</span></h3>
                  </div>
                </div>
                <div class="row invoice-info">
                  <div class="col-xs-12">
                    <span class="col-xs-6"><b>充值通道</b></span>
                    <span class="col-xs-6" id='reg_channel'><b>#1</b></span><br><br>
                    <span class="col-xs-6"><b>充值金额:</b></span>
                    <span class="col-xs-6" id='reg_money'>$302,458</span><br>
                    <span class="col-xs-6"><b>申请日期:</b></span>
                    <span class="col-xs-6" id='reg_date'>2017年6月8日</span></div>
                </div>
              </section>
            <!--/div-->
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" id='reg_nopass_submit' class="btn btn-default" data-dismiss="modal">拒绝</button>
        <button type="button" id='reg_pass_submit' class="btn btn-warning">同意</button>
      </div>
    </div>
  </div>
</div>
<?php
if($mode=='fail'){
$this->beginBlock('prove-lost-js')
?>

  //审核充值
  $('.prove_lost_congzhi').click(function(){
    var $row=$(this).parents('tr');
    var datakey = $row.attr('data-key');
    var $user = $row.find('td:eq(1)');
    var $date=$row.find('td:eq(2)');
    var $money=$row.find('td:eq(4)');
    var $channel = $row.find('td:eq(3)');

    $('#reg_realname').text($user.text());
    $('#reg_date').text($date.text());
    $('#reg_money').text($money.text());
    $('#reg_channel').text($channel.text());
    $('#myModal').attr('data-key',datakey);
    $('#myModal').modal();
  });

  $('#reg_nopass_submit').click(function(){
    $.ajax({
      url:"<?=Url::toRoute('site/congzhiproveajax')?>",
      type:"get",
      datatype:"json",
      cache:false,
      data:{
        datakey:$('#myModal').attr('data-key'),
        pass:0
      },
      success:function(data){
         var obj = JSON.parse(data);
         if(obj.errcode===0){
           var datakey = $('#myModal').attr('data-key');
           var $row = $('tr[data-key="'+datakey+'"]');
           var $prove = $row.find('td:eq(5)');
           $prove.html('<i class="fa fa-lg fa-times-circle alert-danger"></i> 不通过');
            $('#myModal').modal('hide');
         }else{
           $('#myModal').modal('hide');
           alert(obj.errmsg);
         }
      },
      error:function(jqXHR,textStatus,errorThrown){
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
        $('.compose').slideToggle();
      }
    });
  });

  $('#reg_pass_submit').click(function(){
    $.ajax({
      url:"<?=Url::toRoute('site/congzhiproveajax')?>",
      type:"get",
      datatype:"json",
      cache:false,
      data:{
        datakey:$('#myModal').attr('data-key'),
        pass:1
      },
      success:function(data){
         var obj = JSON.parse(data);
         if(obj.errcode===0){
           var datakey = $('#myModal').attr('data-key');
           var $row = $('tr[data-key="'+datakey+'"]');
           var $prove = $row.find('td:eq(5)');
           $prove.html('<i class="fa fa-lg fa-check-circle alert-info"></i> 通过');
            $('#myModal').modal('hide');
         }else{
           $('#myModal').modal('hide');
           alert(obj.errmsg);
         }
      },
      error:function(jqXHR,textStatus,errorThrown){
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
        $('.compose').slideToggle();
      }
    });
  });

<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['prove-lost-js'],\yii\web\View::POS_END);
}
?>
